 //
//  Consume_MapViewController.m
//  JinFuThree
//
//  Created by 爱尚 on 2018/9/6.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "Consume_MapViewController.h"



@interface Consume_MapViewController ()<MKMapViewDelegate>
{
    CLLocationManager *_locationManager;
    MKMapView *_mapView;
    UIView *_bottomView;
    ZYMapNavigationView *_mapNavigationView;
}

@end

@implementation Consume_MapViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    
}

-(void)initUI{
    CGRect rect = [UIScreen mainScreen].bounds;
    _mapView = [[MKMapView alloc]initWithFrame:rect];
    [self.view addSubview:_mapView];
    
    //设置代理
    _mapView.delegate = self;
    //请求定位服务
    _locationManager = [[CLLocationManager alloc]init];
    if (![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus]!= kCLAuthorizationStatusAuthorizedWhenInUse) {
        [_locationManager requestWhenInUseAuthorization];
    }
    //用户位置追踪（用户位置追踪用于标记当前位置，此时会调用定位服务）
    _mapView.userTrackingMode = MKUserTrackingModeFollow;
    
    //设置地图类型
    _mapView.mapType = MKMapTypeStandard;
    
    //设置地图显示范围(如果不进行区域设置会自动显示区域范围并指定当前用户位置为地图中心点)
        MKCoordinateSpan span=MKCoordinateSpanMake(0.01, 0.01);
        MKCoordinateRegion region=MKCoordinateRegionMake(self.destination_LocationCoordinate, span);
        [_mapView setRegion:region animated:true];
    
    //添加大头针
    [self addAnnotation];
    
    _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight-64, ScreenWidthMao, 64)];
    _bottomView.backgroundColor = [UIColor whiteColor];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[UIImage imageNamed:@"BackArrow"] forState:UIControlStateNormal];
    backBtn.frame = CGRectMake(5, 7, 50, 50);
    [backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_bottomView addSubview:backBtn];
    
    
    _shopName = [[UILabel alloc]init];
    _shopName.text = @"小寨张辉oppo手机卖场";
    _shopName.font = [UIFont systemFontOfSize:15];
    _shopName.textColor = HexColor(0x333333);
    _shopName.frame = CGRectMake(55, 13, ScreenWidthMao-65-110, 15);
    [_bottomView addSubview:_shopName];
    
    _addressBtn = [[UILabel alloc]init];
    _addressBtn.text = @"西安市碑林区南稍门中贸广场";
    _addressBtn.font = [UIFont systemFontOfSize:13];
    _addressBtn.textColor = HexColor(0x666666);
    _addressBtn.frame = CGRectMake(55, 36, ScreenWidthMao-65-110, 15);
    [_bottomView addSubview:_addressBtn];
    
    
    UIButton *daohangBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [daohangBtn setImage:[UIImage imageNamed:@"ic_navigation"] forState:UIControlStateNormal];
    [daohangBtn addTarget:self action:@selector(daohangBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [daohangBtn setTitle:@"导航" forState:UIControlStateNormal];
    [daohangBtn setTitleColor:HexColor(0x454545) forState:UIControlStateNormal];
    daohangBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    daohangBtn.frame = CGRectMake(ScreenWidthMao-100, 22, 80, 20);
    [_bottomView addSubview:daohangBtn];
    [daohangBtn layoutButtonWithEdgeInsetsStyle:XNButtonEdgeInsetsStyleLeft imageTitleSpace:5];
    
    [self.view addSubview:_bottomView];
    
    
}
-(void)daohangBtnAction{
    _mapNavigationView = [[ZYMapNavigationView alloc]init];
    [_mapNavigationView showMapNavigationViewWithtargetLatitude:34.219873 targetLongitute:108.959122 toName:@"手机卖场"];
}
-(void)backBtnAction{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark ----添加大头针------------
-(void)addAnnotation{
    CLLocationCoordinate2D location1 = CLLocationCoordinate2DMake(34.219873, 108.959122);
    ZYAnnotation *annotation1 = [[ZYAnnotation alloc]init];
//    annotation1.title = @"店名字";
//    annotation1.subtitle = @"具体位置";
    annotation1.image = [UIImage imageNamed:@"ic_laction"];
    annotation1.coordinate = location1;
    [_mapView addAnnotation:annotation1];
    
    
}

#pragma mark - 地图控件代理方法
#pragma mark 显示大头针时调用，注意方法中的annotation参数是即将显示的大头针对象
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    //由于当前位置的标注也是一个大头针，所以此时需要判断，此代理方法返回nil使用默认大头针视图
    if ([annotation isKindOfClass:[ZYAnnotation class]]) {
        static NSString *key1=@"AnnotationKey1";
        MKAnnotationView *annotationView=[_mapView dequeueReusableAnnotationViewWithIdentifier:key1];
        //如果缓存池中不存在则新建
        if (!annotationView) {
            annotationView=[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:key1];
            annotationView.canShowCallout=true;//允许交互点击
            annotationView.calloutOffset=CGPointMake(0, 1);//定义详情视图偏移量
//            annotationView.leftCalloutAccessoryView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_classify_cafe.png"]];//定义详情左侧视图
        }
        
        //修改大头针视图
        //重新设置此类大头针视图的大头针模型(因为有可能是从缓存池中取出来的，位置是放到缓存池时的位置)
        annotationView.annotation=annotation;
        annotationView.image=((ZYAnnotation *)annotation).image;//设置大头针视图的图片
        
        return annotationView;
    }else {
        return nil;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
